﻿#include "multiSearch.h"
#include <qsciscintilla.h>
#include <QMessageBox>
#include "Scintilla.h"
#include <QDockWidget>

#include <QTextEdit>
#include <QMainWindow>

#pragma execution_character_set("utf-8")


MultiSearchClass::MultiSearchClass(QWidget* parent, QsciScintilla* pEdit)
	: QWidget(parent), mainWidget_(parent)
{
	ui.setupUi(this);
	m_pEdit = pEdit;
	connect(ui.applyButton, SIGNAL(clicked()), this, SLOT(on_applyAPP()));
	dockWidget_ = new QDockWidget(QString::fromUtf8("搜索结果"), NULL);
	dockWidget_->setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
	dockWidget_->setAllowedAreas(Qt::LeftDockWidgetArea);
	dockWidget_->hide();
	auto mainWindow = dynamic_cast<QMainWindow*>(parent);
	mainWindow->addDockWidget(Qt::RightDockWidgetArea, dockWidget_);

}


void readTextFromUi(QLineEdit* controlEdit, char * target1)
{
	QString str = controlEdit->text();
	char* ch;
	QByteArray ba = str.toLatin1();
	ch = ba.data();
	strcpy(target1,ch);
}

void MultiSearchClass::on_applyAPP()
{
	auto scintilla_ = scintillaCallback_();

	const int	size = scintilla_->SendScintilla(SCI_GETLENGTH);

	char* buffer = new char[size + 1];

	scintilla_->SendScintilla(SCI_GETTEXT, size, buffer);
	buffer[size] = 0;

	//char errMsg[256];
	//sprintf(errMsg, "size=%d", size);

	char* buf_found = new char[size + 1];
	memset(buf_found, 0, size + 1);

	char target1[128];
	char target2[128];
	char target3[128];
	char target4[128];
	char target5[128];

	readTextFromUi(ui.lineEdit_1, target1);
	readTextFromUi(ui.lineEdit_2, target2);
	readTextFromUi(ui.lineEdit_3, target3);
	readTextFromUi(ui.lineEdit_4, target4);
	readTextFromUi(ui.lineEdit_5, target5);

	int len_t1 = (int)strlen(target1);
	int len_t2 = (int)strlen(target2);
	int len_t3 = (int)strlen(target3);
	int len_t4 = (int)strlen(target4);
	int len_t5 = (int)strlen(target5);

	//char tmpbuf[2048];

	int start = 0;
	int cur = 0;
	int matched = 0;
	int len_saved = 0;

	while (cur < size)
	{
		int pos_t1 = 0;
		int pos_t2 = 0;
		int pos_t3 = 0;
		int pos_t4 = 0;
		int pos_t5 = 0;
		int found = 0;
		start = cur;
		while (buffer[cur] != '\n' && buffer[cur] != EOF && cur < size)	//process by line
		{
			if (found == 1) //has found, skip the comparation
			{
				cur++;
				continue;
			}

			buffer[cur] == target1[pos_t1] ? (pos_t1++) : (pos_t1 = 0);
			if (len_t1 > 0 && pos_t1 == len_t1)	//Don't break when found, it needs to move to the \n.
				found = 1;

			buffer[cur] == target2[pos_t2] ? (pos_t2++) : (pos_t2 = 0);
			if (len_t2 > 0 && pos_t2 == len_t2)	//Don't break when found, it needs to move to the \n.
				found = 1;

			buffer[cur] == target3[pos_t3] ? (pos_t3++) : (pos_t3 = 0);
			if (len_t3 > 0 && pos_t3 == len_t3)	//Don't break when found, it needs to move to the \n.
				found = 1;

			buffer[cur] == target4[pos_t4] ? (pos_t4++) : (pos_t4 = 0);
			if (len_t4 > 0 && pos_t4 == len_t4)	//Don't break when found, it needs to move to the \n.
				found = 1;

			buffer[cur] == target5[pos_t5] ? (pos_t5++) : (pos_t5 = 0);
			if (len_t5 > 0 && pos_t5 == len_t5)	//Don't break when found, it needs to move to the \n.
				found = 1;

			cur++;
		}

		if (found == 1)
		{
			memcpy(buf_found + len_saved, buffer + start, cur - start + 1);//add 1 at last which is /n
			len_saved += cur - start + 1;
			matched++;
		}

		cur++;
	}

	if (matched > 0)//creat a new window to show the result
	{

		QTextEdit* te1 = new QTextEdit();
		dockWidget_->setWidget(te1);
		te1->setText(tr(""));
		te1->insertPlainText(buf_found);
		dockWidget_->show();
	
	}
	else {
		QMessageBox::warning(NULL, "Warning", "Error Message: result not find");
	}

	free(buffer);
	free(buf_found);
}

void MultiSearchClass::setScintilla(const std::function<QsciScintilla* ()>& cb)
{
	scintillaCallback_ = cb;
}


MultiSearchClass::~MultiSearchClass()
{
}


